package com.snake.springboot.custom.service.impl.aicost;


import com.snake.springboot.custom.api.model.base.CustPointsModel;
import com.snake.springboot.custom.api.model.aicost.GeneratePayModel;
import com.snake.springboot.custom.api.model.common.AccountType;
import com.snake.springboot.custom.api.model.points.PointsChangeModel;
import com.snake.springboot.custom.api.response.aicost.GenerateCheckRsp;
import com.snake.springboot.custom.api.service.aicost.GeneratePayService;
import com.snake.springboot.custom.api.service.base.CustPointsService;
import com.snake.springboot.custom.dao.mybatis.entity.aicost.FormulaDO;
import com.snake.springboot.custom.dao.mybatis.entity.aicost.GenerateFlowDO;
import com.snake.springboot.custom.dao.mybatis.mappers.aicost.FormulaMapper;
import com.snake.springboot.custom.dao.mybatis.mappers.aicost.GenerateFlowMapper;
import com.snake.springboot.custom.service.assemble.aicost.GeneratePayAssembler;
import org.snake.common.security.account.AccountData;
import org.snake.common.security.account.LoginAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * <p>生成支付业务类
 * 类{@code GeneratePayServiceImpl} </br>
 * 创建时间2025/3/21 10:28
 * @author yangxt
 * @see
 */
@Service
public class GeneratePayServiceImpl implements GeneratePayService {

    private FormulaMapper formulaMapper;
    private GenerateFlowMapper flowMapper;
    private CustPointsService custPointsService;
    @Autowired
    public GeneratePayServiceImpl(FormulaMapper formulaMapper, GenerateFlowMapper flowMapper, CustPointsService custPointsService) {
        this.formulaMapper = formulaMapper;
        this.flowMapper = flowMapper;
        this.custPointsService = custPointsService;
    }

    @Override
    public GenerateCheckRsp check(GeneratePayModel model) {
        AccountData account = LoginAccount.getAccount();
        if(AccountType.EMP.equals(account.getType())){
            return GenerateCheckRsp.ucc();
        }

        //查询用算力
        FormulaDO formulaDO = formulaMapper.findByFormulaCode(model.getFormulaCode());
        if (formulaDO == null) {
            return GenerateCheckRsp.fail("当前生成不存在");
        }
        //如果费用是0 则直接返回
        if (BigDecimal.ZERO.compareTo(formulaDO.getPoints())==0){
            return GenerateCheckRsp.ucc();
        }
        //查算力
        CustPointsModel result = custPointsService.queryById(model.getCustomId());
        if (result.getPoints().compareTo(formulaDO.getPoints())<0){
            return GenerateCheckRsp.fail("算力不足:"+formulaDO.getPoints());
        }
        return GenerateCheckRsp.ucc();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void pay(GeneratePayModel model) {
        //如果是员工则不需要支付
        AccountData account = LoginAccount.getAccount();
        if(AccountType.EMP.equals(account.getType())){
            return;
        }
        //查询用算力
        FormulaDO formulaDO = formulaMapper.findByFormulaCode(model.getFormulaCode());
        //如果费用是0 则不需要去支付
        if (BigDecimal.ZERO.compareTo(formulaDO.getPoints())==0){
            return ;
        }
        GenerateFlowDO generateFlowDO = GeneratePayAssembler.toFlowDO(model,formulaDO);
        flowMapper.insert(generateFlowDO);
        //扣算力
        PointsChangeModel changeModel = GeneratePayAssembler.toPointsChangeModel(model,formulaDO);
        custPointsService.reduce(changeModel);

    }
}
